package com.lagou.rabbitmq.demo.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitConfig {

    // 数据一过来，往db写数据，未支付数据
    @Bean
    public Queue unPayedQueue() {
        return new Queue("queue.biz.unpay", false, false, false, null);
    }
    @Bean
    public Queue queueDLX() {
        return new Queue("queue.dlx", false, false, false, null);
    }
    @Bean
    public Exchange dlxExchange() {
        return new DirectExchange("exchange.dlx", true, false);
    }
    @Bean
    public Exchange exchange() {
        return new FanoutExchange("exchange.biz", true, false, null);
    }
    // 做延迟的队列
    @Bean
    public Queue queueBiz() {
        Map<String, Object> args = new HashMap<>();
        // 等待10秒支付
        args.put("x-message-ttl",10000);
        args.put("x-dead-letter-exchange","exchange.dlx");
        args.put("x-dead-letter-routing-key","key.dlx");
        return new Queue("queue.biz.topay", false, false, false, args);
    }

    @Bean
    public Binding dlxBinding() {

        return new Binding("queue.dlx",
                Binding.DestinationType.QUEUE,
                "exchange.dlx",
                "key.dlx",
                null);
    }
    @Bean
    public Binding bindingUnPay() {
        // 超时未支付的订单
        return new Binding("queue.biz.unpay",
                Binding.DestinationType.QUEUE,
                "exchange.biz",
                "key.biz",
                null);
    }

    @Bean
    public Binding bindingToPay() {
        // 等待支付的订单
        return new Binding("queue.biz.topay",
                Binding.DestinationType.QUEUE,
                "exchange.biz",
                "key.biz",
                null);
    }

}
